package com.darrenchan;

import java.math.BigInteger;
import java.util.Arrays;

public class TestDemo {
    public static void main(String[] args) {
        BigInteger a1 = BigInteger.valueOf(1);
        BigInteger a2 = BigInteger.valueOf(1);

        for (int i = 1; i <= 49; i++) {
            a1 = a1.multiply(BigInteger.valueOf(i));
            a2 = a2.multiply(BigInteger.valueOf(i + 50));
        }

        System.out.println(a2.divide(a1));
    }


    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int ans = 0;

        for (int i = 0; i < nums.length; i++) {
            int left = 0, right = i - 1;
            while(left < right){
                if(nums[left] + nums[right] > nums[i]){
                    ans += right - left;
                    right--;
                }else{
                    left++;
                }
            }
        }

        return ans;
    }


    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] temp = new int[m + n];
        int left = 0, right = 0;
        int index = 0;
        while(left < m && right < n){
            if(nums1[left] < nums2[right]){
                temp[index++] = nums1[left++];
            }else{
                temp[index++] = nums2[right++];
            }
        }
        while(left < m){
            temp[index++] = nums1[left++];
        }

        while(right < n){
            temp[index++] = nums2[right++];
        }

        System.arraycopy(temp, 0, nums1, 0, m + n);

    }

    public int threeSumClosest(int[] nums, int target) {
        if(nums == null || nums.length == 0){
            return 0;
        }

        Arrays.sort(nums);

        int diff = Integer.MAX_VALUE;
        int res = 0;

        for (int i = 0; i < nums.length; i++) {
            int left = 0, right = i - 1;
            while(left < right){
                int sum = nums[left] + nums[right] + nums[i];
                if(sum < target){
                    if(target - sum < diff){
                        diff = target - sum;
                        res = sum;
                    }
                    left++;
                }else{
                    if(sum - target < diff){
                        diff = sum - target;
                        res = sum;
                    }
                    right--;
                }
            }
        }
        return res;
    }
}
